package com.itboye.mengwu.app.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.utils.XImageLoader;
import com.itboye.inageview.RoundImageView;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.logincontroller.LoginController;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;

public class MyAdapter extends BaseAdapter {
    Context context;
    ArrayList<TieZiBaoHanReply.GetAList> aLists = new ArrayList<TieZiBaoHanReply.GetAList>();
    private OnClickListener dianzanListener;

    public MyAdapter(Context context,ArrayList<TieZiBaoHanReply.GetAList> aLists,OnClickListener dianzanListener) {
        this.context = context;
        this.aLists=aLists;
       this.dianzanListener=dianzanListener;
    }

    @Override
    public int getCount() {
        return aLists == null ? 0 : aLists.size();
    }


	@Override
	public Object getItem(int position) {
		return aLists == null ? 0 : aLists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("unused")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.mytiezi_list_item, null);

			holder.question_show = (TextView) convertView
					.findViewById(R.id.question_show);
			holder.author_headewrshow = (ImageView) convertView
					.findViewById(R.id.author_headewrshow);
			holder.baby_sex = (TextView) convertView
					.findViewById(R.id.baby_sex);
			holder.baby_years = (TextView) convertView
					.findViewById(R.id.baby_years);
			holder.imageContainer = (LinearLayout) convertView
					.findViewById(R.id.imageContainer);
			holder.pinglunderen = (LinearLayout) convertView
					.findViewById(R.id.pinglunderen);
			holder.biaoqian_lin = (LinearLayout) convertView
					.findViewById(R.id.biaoqian_lin);
			 holder.pinglun = (TextView) convertView
			 .findViewById(R.id.pinglunrenshu);
			 holder.imageContainer=(LinearLayout) convertView.findViewById(R.id.imageContainer);
			// holder.pinglun = (MyListView) convertView
			// .findViewById(R.id.pinglun);
			holder.author_headewrshow = (ImageView) convertView
					.findViewById(R.id.author_headewrshow);
			 holder.linlayouShouCang = (LinearLayout) convertView
			 .findViewById(R.id.linlayouShouCang);
			 holder.linlayoutPinLun=(LinearLayout) convertView.findViewById(R.id.linlayoutPinLun);
//			holder.hot_talk = (TextView) convertView
//					.findViewById(R.id.hot_talk);
			holder.collection_num=(TextView) convertView.findViewById(R.id.collection_num);
			holder.imgMytiezi=(ImageView) convertView.findViewById(R.id.imgMytiezi);
			holder.img_1 = (ImageView) convertView.findViewById(R.id.img_1);
			holder.img_2 = (ImageView) convertView.findViewById(R.id.img_2);
			holder.img_3 = (ImageView) convertView.findViewById(R.id.img_3);
			holder.author_name = (TextView) convertView
					.findViewById(R.id.author_name);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (aLists.get(position).getMessage() != null) {
			holder.question_show.setText(aLists.get(position).getMessage());
		}
		holder.author_name.setText(aLists.get(position).getAuthor());
		holder.img_1.setImageBitmap(null);
		holder.img_2.setImageBitmap(null);
		holder.img_3.setImageBitmap(null);
		if (aLists.get(position).getImg() != null) {
			if (aLists.get(position).getImg().size() < 0) {
				// XImageLoader.load(GlobalConfig.API_URL
				// + "/picture/index?id="
				// + aLists.get(position).getImg().get(0),
				// holder.img_1);
				holder.img_1.setVisibility(View.GONE);
				holder.img_2.setVisibility(View.GONE);
				holder.img_3.setVisibility(View.GONE);
				holder.imageContainer.setVisibility(View.GONE);
			}

			if (aLists.get(position).getImg().size() > 0) {
				XImageLoader.load(GlobalConfig.API_URL
						+ "/picture/index?id="
						+ aLists.get(position).getImg().get(0),
						holder.img_1);
				holder.img_1.setVisibility(View.VISIBLE);
				holder.img_2.setVisibility(View.GONE);
				holder.img_3.setVisibility(View.GONE);
				holder.imageContainer.setVisibility(View.VISIBLE);
			}
			if (aLists.get(position).getImg().size() > 1) {
				XImageLoader.load(GlobalConfig.API_URL
						+ "/picture/index?id="
						+ aLists.get(position).getImg().get(1),
						holder.img_2);
				holder.img_1.setVisibility(View.VISIBLE);
				holder.img_2.setVisibility(View.VISIBLE);
				holder.imageContainer.setVisibility(View.VISIBLE);
				holder.img_3.setVisibility(View.GONE);
			}
			if (aLists.get(position).getImg().size() > 2) {
				XImageLoader.load(GlobalConfig.API_URL
						+ "/picture/index?id="
						+ aLists.get(position).getImg().get(2),
						holder.img_3);
				holder.img_1.setVisibility(View.VISIBLE);
				holder.img_2.setVisibility(View.VISIBLE);
				holder.img_3.setVisibility(View.VISIBLE);
				holder.imageContainer.setVisibility(View.VISIBLE);
			}
		}
		System.out.println(aLists.get(position).getImg());

		holder.pinglunderen.removeAllViews();
		for (int i = 0; i < aLists.get(position).getReplyInfo().getList()
				.size(); i++) {
			RoundImageView imageView = new RoundImageView(context);
			LayoutParams layoutParams = new LayoutParams(80, 100);
			layoutParams.setMargins(5, 5, 5, 5);
			if (!aLists.get(position).getReplyInfo().getList().get(i)
					.getHead().startsWith("http"))
				XImageLoader.load(GlobalConfig.API_URL
						+ "/picture/index?id="
						+ aLists.get(position).getReplyInfo().getList()
								.get(i).getHead(), imageView);
			else
				XImageLoader.load(aLists.get(position).getReplyInfo()
						.getList().get(i).getHead(), imageView);
			holder.pinglunderen.addView(imageView, layoutParams);
		}
		holder.pinglun.setText(aLists.get(position).getReplyInfo()
				.getCount());// 评论人数
		holder.collection_num.setText(aLists.get(position).getFavs());
		holder.imgMytiezi.setTag(position);
		
		holder.baby_years.setText(aLists.get(position).getDatelineShow());
		if(holder.baby_years==null){
			holder.baby_years.setText("宋慧乔");
		}
		holder.baby_sex.setTag(aLists.get(position).getauthorId());

	
		if (!aLists.get(position).getHead().startsWith("http:")) {
			XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
					+ aLists.get(position).getHead(),
					holder.author_headewrshow);
		} else {
			XImageLoader.load(aLists.get(position).getHead(),
					holder.author_headewrshow);
		}
		if(Integer.parseInt(aLists.get(position).getIsLike())==1){
			holder.imgMytiezi.setImageResource(R.drawable.collection_yi);
			holder.imgMytiezi.setTag(R.id.tag_first,"1");
			ByAlert.alert("已收藏");
		}else{
			holder.imgMytiezi.setImageResource(R.drawable.collection_un);
			ByAlert.alert("已取消收藏");
			holder.imgMytiezi.setTag(R.id.tag_first,"0");
		}
		holder.imgMytiezi.setTag(position);
		holder.imgMytiezi.setOnClickListener(dianzanListener);
		holder.linlayouShouCang.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				LoginController.onlickShouCang(context,
						null);
			}
		});
		return convertView;
	}

	class ViewHolder {
		TextView author_name,  baby_years, baby_sex;
		TextView question_show, hot_talk;
		LinearLayout imageContainer, pinglunderen, biaoqian_lin;
		ImageView author_headewrshow, img_1, img_2, img_3;
		LinearLayout linlayouShouCang;
		private ImageView imgMytiezi;
		LinearLayout linlayoutPinLun;
		TextView collection_num,pinglun;
	}
}